﻿using SmartRS.com.vn.smartrs.domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;

namespace SmartRS.com.vn.smartrs.dao
{
    public class HallDao : BaseDao<Hall>
    {
        private static HallDao _hallDao = new HallDao();
        public static String TABLE_NAME = "Hall";
        public static String HALL_ID = "hallId";
        public static String HALL_NAME = "hallName";
        public static String STATUS = "status";


        private HallDao()
        { }

        public static HallDao getInstance()
        {
            return _hallDao;
        }

        public IList<Hall> getListHallByStatus(int status)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                try
                {
                    var result = session.QueryOver<Hall>().Where(x => x.status == status && x.deteleFlag == false).List<Hall>();
                    if (result != null && result.Count > 0)
                    {
                        return result.ToList();
                    }
                    return new List<Hall>();
                }
                catch (Exception ex)
                {
                    throw new Exception("getListHallByStatus happend error :" + ex.Message);
                }
            }
        }

        public bool updateHallInfoById(long hallId, Hall hall)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {

                        var query = "update Hall set hallName = :name, status = :status where hallId = :id";
                        var update = session.CreateQuery(query)
                                            .SetParameter("name", hall.hallName)
                                            .SetParameter("status", hall.status)
                                            .SetParameter("id", hallId);
                        update.ExecuteUpdate();

                        transaction.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        if (transaction.WasCommitted)
                        {
                            transaction.Rollback();
                            Console.WriteLine("Delete commited error: " + ex.Message);
                            return false;
                        }
                        throw new Exception("updateHallInfoById happened error: " + ex.Message);
                    }
                }
            }
        }

        public IList<Hall> getAllOrderById()
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                try
                {
                    return session.CreateCriteria<Hall>().
                        AddOrder(new NHibernate.Criterion.Order(HALL_ID, true)).List<Hall>().Where(x => x.deteleFlag == false).ToList<Hall>();
                }
                catch (Exception ex)
                {
                    throw new Exception("getAllOrderById happend error :" + ex.Message);
                }
            }
        }
    }
}
